package com.qs.javame.thread.threadlocal;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class TestDemo {

    private static ThreadLocal<String> threadLocal = new ThreadLocal<>();

    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        for (int i = 0; i < 20; i++) {
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    String value = threadLocal.get();
                    if (value == null) {
                        threadLocal.set("aaa-" + Thread.currentThread().getId());
                        System.out.println("2、线程：" + Thread.currentThread() + "：" + threadLocal.get());
                        //获取过线程变量之后，将存储在threadLocal中线程变量移除掉
                        threadLocal.remove();
                    } else {
                        System.out.println("1、线程：" + Thread.currentThread() + "：" + value);
                    }
                }
            });
        }

        executorService.shutdown();
    }
}


